package com.bristua.mall.user.biz;

import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.enums.UserCode;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.condition.TUserExample;
import com.bristua.mall.user.model.condition.userReg.UserReg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/3/30.
 */
@Service
@Transactional
public class UserRegBiz {

    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TUserAccountMapper tUserAccountMapper;

    public void userReg(UserReg userReg) {
        if (!StringUtils.isEmpty(userReg.getPhone())) {
            TUserExample tUserExample = new TUserExample();
            tUserExample.createCriteria().andPhoneEqualTo(userReg.getPhone());
            List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
            if (tUsers != null && tUsers.size() > 0) {
                throw new BizException(UserCode.USER_ISEXIST.getCode(), UserCode.USER_ISEXIST.getMsg());
            }
        }
        String parentId = "";
        if (!StringUtils.isEmpty(userReg.getInviteCode())) {
            TUserExample tUserExample = new TUserExample();
            tUserExample.createCriteria().andPhoneEqualTo(userReg.getInviteCode());
            List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
            if (tUsers != null && tUsers.size() > 0) {
                TUser tUser = tUsers.get(0);
                parentId = tUser.getUserId();
            }
        }


        TUser tUser = new TUser();
        tUser.setUserId(SequenceUtil.nextId());
        tUser.setPhone(userReg.getPhone());
        tUser.setParentId(parentId);
        tUser.setCreateTime(DateUtil.getDateTime3());
        tUser.setUserName(userReg.getPhone());
        tUser.setStatus("1");
        tUser.setUserCode(createUserCode());
        tUser.setUserLevel("0");

        TUserAccount tUserAccount = new TUserAccount();
        tUserAccount.setUserAccountStatus("1");
        tUserAccount.setUserAccountId(SequenceUtil.nextId());
        tUserAccount.setUserId(tUser.getUserId());
        tUserAccount.setUserIntegral("0");
        tUserAccountMapper.insert(tUserAccount);
        tUserMapper.insert(tUser);

    }

    private String createUserCode() {
        String s = SequenceUtil.generateShortUuid();
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUserCodeEqualTo(s);
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if (tUsers == null || tUsers.size() == 0) {
            return s;
        }
        createUserCode();
        return null;

    }
}
